/*
 * Part of knowledge engineering (ke) course work, 2010/11
 */
package se.bth.ke.firstfriend.model;

import de.nixis.commons.web.base.model.UserBase;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @TODO: Use the validator annotations @SingleUser and @ValidPassword for
 * User validation instead of controller based (see UserController).
 *
 * Reason for this MVC violation: bug in hibernate validator package.
 * Update it beforehand.
 *
 * @author markus.goetz, nico.rehwaldt
 */
@Table(name = "system_user")
@Entity
@NamedQueries({
    @NamedQuery(
        name="User.GetByName",
        query="SELECT u FROM User u WHERE u.name = :name")
})
public class User extends UserBase implements Serializable {

    private String firstName;

    private String lastName;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="user")
    private List<Bookmark> bookmarks;
    
    @OneToMany(cascade={CascadeType.PERSIST, CascadeType.REMOVE}, mappedBy="user")
    private List<Todo> todos;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="user")
    private List<Rating> ratings;
    
    public User() {}
    
    public User(String name, String firstName, String lastName,
                String email, String password, String passwordVerification) {
        
        super(name, email, password, passwordVerification);
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }
    
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void addBookmark(Bookmark bookmark) {
        if (bookmarks == null) {
            bookmarks = new ArrayList();
        }

        bookmarks.add(bookmark);
    }
    
    @Override
    public String toString() {
        return "se.bth.ke.firstfriend.model.User[id=" + getId() + "]";
    }

    @Override
    public boolean isAdmin() {
        return getName().equals("admin");
    }
}
